/**
 * @author:  原晋川
 * @date:    2019/9/23 17:07
 */
angular.module(APP_NAME).directive("relevance", ['$http', function ($http) {
    return {
        restrict: 'E',
        scope: {
            model: "=?ngModel",
            modelSelect: "=?ngModelSelect",
            multiple: "@multiple",
            disabled: "=?ngDisabled",
            // dataField: "=ngDatafield",
            // dataUrl: "@ngDataUrl",
            initValue: "@ngInitValue",
            modelValue: "=?ngModelValue",
            relevanceType: "@ngRelevanceType",
            // position: "@ngPosition",
            callback: "&ngCallback",
            splitType: "@ngSplit",
            searchEnabled: "@searchEnabled",
            selectTip: "@selectTip",
            resultValue: "=?ngResultValue"
        },
        template: function (tEle, tAttr) {
            return '<div>' +
                '<button style="color: #fff;background-color: #3492ea;padding: 0 12px;height: 28px;line-height: 25px;border: none;font: 14px \'Microsoft YaHei\', Arial, Helvetica, sans-serif" ng-click="addRelevance()">添加关系</button>' +
                '</div>' +
                '<ul ng-repeat="relevance in relevances">' +
                '<li>【{{relevance.YWLXMC}}】 {{relevance.YWBT}} <a href="javascript:void(0)" style="float: right" ng-click="deleteRelevanceBusiness(relevance.XH)" >解除关联</a></li>'
        },
        link: function ($scope, $element, attr) {
            $scope.relevance = {};
            $scope.relevance.multiple = false;
            //默认单选  当等于true时，表示支持多选
            if ($scope.multiple !== '' && $scope.multiple === true) {
                $scope.relevance.multiple = $scope.multiple;
            }

            //initValue在标签上存在初始化值时、且model与modelValue均无初始化值时。
            if ($scope.initValue && !$scope.model && !$scope.modelValue) {
                $scope.modelValue = $scope.initValue;
            }
            // $scope.BUSINESSUUID
            $scope.onlyRead = false;

            $scope.$watch("disabled", function (newValue) {
                switch ($scope.disabled) {
                    case "false" :
                        $scope.onlyRead = false;
                        break;
                    case "true" :
                        $scope.onlyRead = true;
                        break;
                    default :
                        $scope.onlyRead = $scope.disabled;
                        break;
                }
            });

            //关联类型
            $scope.relevance.relevanceType = []
            console.log($scope.relevanceType)
            if ($scope.relevanceType) {
                if ($scope.relevanceType.indexOf(",") !== -1) {
                    $scope.relevance.relevanceType = $scope.relevanceType.split(",")
                }else {
                    $scope.relevance.relevanceType.push($scope.relevanceType)
                }
            }

            $scope.relevance.modelValue = []
            if ($scope.modelValue ) {
                if ($scope.modelValue.indexOf(",") !== -1) {
                    $scope.relevance.modelValue = $scope.modelValue.split(",")
                }else {
                    $scope.relevance.modelValue.push($scope.modelValue)
                }
            }
            
            if ($scope.model) {
                queryRelevanceBusiness();
            }

            $scope.addRelevance = function () {
                // $scope.relevance = JSON.stringify($scope.relevance) + $scope.relevance
                Common.dialog({
                    url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/showList/'+$scope.model+'/'+$scope.relevanceType+'/'+$scope.multiple,
                    title: '添加关联关系',
                    type: 'open',
                    width: '50%',
                    height: '70%',
                    skin: 'skin-default-class',
                    end: function () {
                        queryRelevanceBusiness();
                    }
                });
            }
            $scope.deleteRelevanceBusiness = function (xh) {
                Common.send($scope, $http ,{
                    method: "POST",
                    url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/deleteRelevanceBusiness',
                    data: {"XH": xh},
                    showTips : true,
                    success: function(data){
                        queryRelevanceBusiness();
                    }
                });

            }
           function queryRelevanceBusiness() {
               var send = {
                   method: 'POST',
                   url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/queryDataByBusiness',
                   data: {"LYYWXH": $scope.model},
                   success: function (data) {
                       $scope.relevances = data.data;
                       $scope.resultValue = data.data;
                       // $scope.relevance.modelValue = $scope.relevances.businessId;
                   },
                   error: function () {
                       console.error("查询出错", send)
                   }
               }
               Common.send($scope, $http, send)
           }
        }
    };
}]);